package databases

import (
	"gitlab.gaodun.com/golib/consul"
	"sync"
	"github.com/imroc/req"
	"encoding/json"
	"permission/vigo_loger"
	"time"
)

const CLIENT_HOST  =  "apigateway.gaodun.com"

var p sync.RWMutex

type ClientResponse struct {
	HttpCode int `json:"http_code"`
	Status int	`json:"status"`
	Info string `json:"info"`
	Result []Tpo_Sys_Client `json:"result"`
}


func FindClient() []Tpo_Sys_Client {
	p.Lock()
	env := consul.GetEnvPath()
	clientUrl := "https://" + env + CLIENT_HOST + "/api/v1/app/get"
	p.Unlock()
	defer func() {
		if err := recover(); err != nil {
			vigo_loger.AddGrayLog(err.(error).Error())
		}
	}()
	req.SetTimeout(3 * time.Second)
	header := req.Header{"Origin": "permission"}
	res, _ := req.Get(clientUrl, header)

	clientRes := ClientResponse{}
	err := json.Unmarshal(res.Bytes(), &clientRes)
	if err != nil {
		vigo_loger.AddGrayLog(err.Error())
	}

	return clientRes.Result
}


